<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class news extends CI_Controller
{
  protected $_templates;
  
	function __construct() 
	{
		parent::__construct();
		$this->load->model('news_model','news');
		$this->pre_message = "";
		$this->load->library('editor_library');
		$this->load->helper('str_helper');
		$this->load->helper('img_helper');
		//  $this->load->config('config_mnews'); 
		//$this->mnews = $this->config->item('mnews'); 
		// $this->session->set_userdata(array('Url'=>uri_string()));           
	}
  
/*  function index()
  {
	  $data['title'] = "Danh mục tin tức";
	  $data['add'] = 'news/addcat';
	  $config['base_url'] = base_url().'news/index/';  
	  $config['total_rows']   =  $this->news->getNumcat();
	  $data['num'] = $config['total_rows'];
	  $config['per_page']  =   20;
	  $config['uri_segment'] = 3; 
	  $this->pagination->initialize($config);   
	  $data['list'] =   $this->news->getAllCat($config['per_page'],$this->uri->segment('3'));
	  $data['pagination']    = $this->pagination->create_links(); 
	  $this->_templates['page'] = 'news/cat/index';
	  $this->site_library->load($this->_templates['page'],$data);         
  }
  function addcat()
  {
	  $data['title'] = 'Thêm mới chuyên mục';
	  $data['listcat'] = $this->news->get_Parent(0);
	  if(!empty($_POST['bt_submit'])){
			$this->form_validation->set_rules('CatName','Tên chủ đề','trim|required');
	 
			if($this->form_validation->run() == FALSE){
				$this->pre_message = validation_errors();
			}else{
				if($this->news->saveUpdateCat()){
				$this->session->set_flashdata('message','Thêm mới chuyên mục thành công');
				redirect('news');
			}
		 }
	  }       
	  $this->_templates['page'] = 'news/cat/addcat';
	  $this->site_library->load($this->_templates['page'],$data);           
  }
  
  function updatecat()
  {                                          
	  $data['rs'] = $this->news->getItemCat();
	  $data['title'] = 'Cập nhật: '.$data['rs']->CatName;
	  $data['listcat'] = $this->news->get_Parent(0);
	  if(!empty($_POST['bt_submit']))
	  {
			$this->form_validation->set_rules('CatName','Tên chủ đề','trim|required');
   
			if($this->form_validation->run() == FALSE){
				$this->pre_message = validation_errors();
			}else{
				if($this->news->saveUpdateCat()){
				$this->session->set_flashdata('message','Cập nhật chuyên mục thành công');
				redirect('news');
			}
		 }
	  }  
	  $this->_templates['page'] = 'news/cat/update';
	  $this->site_library->load($this->_templates['page'],$data);
  }
  
  function delcat()
  {
	  $id = $this->uri->segment(3);
	  $page = $this->uri->segment(4);
		if($this->news->delcat($id))
			$this->session->set_flashdata('mesasge','Đã xóa thành công');
		else $this->session->set_flashdata('mesasge','Xóa không thành công');
	  redirect('news/index/'.$page);
  }
  
  function delscat()
  {
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->news->delcat($ar_id[$i]))
							$this->session->set_flashdata('mesasge','Đã xóa thành công');
						else $this->session->set_flashdata('mesasge','Xóa không thành công');
					}
				}
			}
		}
		redirect('news/index/'.$page);
  }
*/  
  // Bengin list news
  function listnews()
  {
	  $data['title'] = 'Danh sách bài viết';
	 $data['add'] = 'news/addnews';
	 
	  $config['base_url'] = base_url().'news/listnews/'.(int)$this->uri->segment(3);  
	  $data['num'] = $this->news->getNumNews();
	  $config['total_rows']   =  $data['num'];
	  $config['per_page']  =   '20';
	  $config['uri_segment'] = 4;   
	  $this->pagination->initialize($config);   
	  $data['list'] =   $this->news->getAllNews($config['per_page'],(int)$this->uri->segment('4'));
	  $data['pagination']    = $this->pagination->create_links();            
	 
	  $this->_templates['page'] = 'news/list/index';
	  $this->site->load($this->_templates['page'],$data);
  }

  
  function addnews()
  {
	  $data['title'] = 'Thêm mới bài viết';
	  //$data['postcat'] = $this->news->get_Parent(0);

		  $this->form_validation->set_rules('ContentTitle','Tiêu đề','trim|required');
		  $this->form_validation->set_rules('ContentImg','','');
		  $this->form_validation->set_rules('ContentSmall','','');
		  $this->form_validation->set_rules('ContentBody','','');
		  if($this->form_validation->run()== FALSE){
			  $this->pre_message = validation_errors();
		  }else{
			  if($this->news->savenews()){
				 if($this->input->post('ContentImg')!=''){
					 if(file_exists('../uploads/news/'.$this->input->post('ContentImg'))){
						 unlink('../uploads/news/'.$this->input->post('ContentImg'));
					 }
					fn_resize_image('../uploads/images/'.$this->input->post('ContentImg'),'../uploads/news/'.$this->input->post('ContentImg'),160,120); 
				  }                      
				  $this->session->set_flashdata('message','Lưu thành công');
				  redirect('news/listnews');
			  }
		  }

	  $data['message'] = $this->pre_message;
	  $this->_templates['page'] = 'news/list/add';
	  $this->site->load($this->_templates['page'],$data);
  }
  
  function updatenews()
  {
	  $data['rs'] = $this->news->getItemNews();
	  $data['title'] = 'Cập nhật: '.$data['rs']->ContentTitle;
	  $data['postcat'] = $this->news->get_Parent(0);
		  $this->form_validation->set_rules('ContentTitle','Tiêu đề','trim|required');
		  $this->form_validation->set_rules('ContentImg','','');
		  $this->form_validation->set_rules('ContentSmall','','');
		  $this->form_validation->set_rules('ContentBody','','');
		  if($this->form_validation->run()== FALSE){
			  $this->pre_message = validation_errors();
		  }else{
			  if($this->news->savenews())
			  {
				 if($this->input->post('ContentImg')!=''){
					 if(file_exists('../uploads/news/'.$this->input->post('ContentImg'))){
						 unlink('../uploads/news/'.$this->input->post('ContentImg'));
					 }
					fn_resize_image('../uploads/images/'.$this->input->post('ContentImg'),'../uploads/news/'.$this->input->post('ContentImg'),160,120); 
				  }                      
				  $this->session->set_flashdata('message','Lưu thành công');
				  redirect('news/listnews');
			  }
		  }

	  $data['message'] = $this->pre_message;
	  $this->_templates['page'] = 'news/list/update';
	  $this->site_library->load($this->_templates['page'],$data);
  }
  
  function delnews()
  {
	  $id = $this->uri->segment(3);
	  $page = $this->uri->segment(4);
		if($this->news->delnews($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
	  redirect('news/listnews/'.$page);
  }
  
  function delsnews()
  {
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->news->delnews($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('news/listnews/'.$page);
  }    
  
      
}
?>
